package com.powernode.service.impl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.domain.SmsLog;
import com.powernode.mapper.SmsLogMapper;
import com.powernode.model.SmsModel;
import com.powernode.model.WxMsgModel;
import com.powernode.service.SmsLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
*内容: 
*作者: cy
*/
@Service
public class SmsLogServiceImpl extends ServiceImpl<SmsLogMapper, SmsLog> implements SmsLogService{

    @Autowired
    private SmsLogMapper smsLogMapper;

    /**
     * 记录阿里大于发送短信
     *
     * @param sendSmsResponse
     * @param smsModel
     */
    @Override
    public void saveSmsLog(SendSmsResponse sendSmsResponse, SmsModel smsModel) {
        SmsLog smsLog = SmsLog.builder()
                .mobileCode(smsModel.getTemplateParam())
                .userPhone(smsModel.getPhoneNumbers())
                .recDate(new Date())
                .status(sendSmsResponse.getCode().equalsIgnoreCase("ok") ? 1 : 0)
                .responseCode(sendSmsResponse.getCode())
                .type(1)
                .content(smsModel.getTemplateCode())
                .build();
        smsLogMapper.insert(smsLog);
    }

    /**
     * 记录发送微信公众号消息
     *
     * @param wxMsgModel
     * @param result
     */
    @Override
    public void saveWxMsgLog(WxMsgModel wxMsgModel, ResponseEntity<String> result) {
        SmsLog smsLog = SmsLog.builder()
                .userId(wxMsgModel.getToUser())
                .recDate(new Date())
                .status(result.getStatusCodeValue() == 200 ? 1 : 0)
                .responseCode(result.getBody())
                .type(1)
                .content(wxMsgModel.getTemplateId())
                .build();
        smsLogMapper.insert(smsLog);
    }
}
